package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user/ShoppingCard")
@Api(tags = "用户端-购物车")
@Slf4j
public class ShoppingCardController {

    @Autowired
    private ShoppingCartService shoppingCardService;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result addShoppingCard(@RequestBody  ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车，商品信息为" + shoppingCartDTO);
        // 添加购物车
        shoppingCardService.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查询购物车
     *
     *
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public  Result<List<ShoppingCart>> list() {
        List<ShoppingCart> list = shoppingCardService.list();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clear")
    @ApiOperation("清空购物车")
    public Result<String> clear() {
        shoppingCardService.clear();
        return Result.success();
    }
    /**
     * 缩减数量（数量为1时执行删除操作）
     *
     */
    @PostMapping("/delete")
    @ApiOperation("减少购物车")
    public Result delete(@RequestBody  ShoppingCartDTO shoppingCartDTO) {
        log.info("减少购物车");
        shoppingCardService.delete(shoppingCartDTO);
        return Result.success();
    }

}
